package educar.models;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import educar.db.DbConnection;
import educar.db.JPA;


public class Correccion {
	private static JPA jpa = new JPA();
	private Integer nota;
	private String dni;
	private Integer id_act;
	private Integer id_solucion;
	
	public Correccion (Integer nota, String dni,Integer id_act,Integer id_solucion){
		this.setNota(nota);
		this.setDni(dni);
		this.setIdAct(id_act);
		this.setIdSolucion(id_solucion);
	}
	
	
	public boolean save() {
		try {
			Correccion.getCorreccionByCorreccion(dni);
			return false;
		} catch (datoNotFound e1) {
			String[] columns = {"nota","dni","id_act","id_solucion"};

			PreparedStatement stm = jpa.newRecord("educar_dev.correccion", columns);

			try {
				stm.setString(1, Integer.toString(nota));
				stm.setString(2, dni);
				stm.setString(3, Integer.toString(id_act));
				stm.setString(4, Integer.toString(id_solucion));
			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			}

			jpa.create(stm);
			return true;
		}
	}
	
	public static Correccion getCorreccionByCorreccion(String dni) throws datoNotFound {
		ResultSet rs = null;
		Correccion p = null;

		rs = jpa.getByField("correccion", "dni", dni);

		try {
			if (rs.next()) {
				p = new Correccion(Integer.parseInt(rs.getString(1)), rs.getString(2),Integer.parseInt(rs.getString(3)),Integer.parseInt(rs.getString(4)));
			} else {
				throw new datoNotFound();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return p;
	}

	public void destroy(String dni,String id_act,String id_solucion) {
		String query= "DELETE from " + DbConnection.bd + ".correccion"; 
    	query += " Where dni= '" + dni + "' and id_act= '"+id_act+"' and id_solucion= '"+id_solucion+"'";
		jpa.runUpdate(query);
	}

	
	public void setNota(Integer nota){
		this.nota=nota;
	}
	
	public Integer getNota(){
		return this.nota;
	}
	
	public void setDni(String dni){
		this.dni=dni;
	}
	
	public String getDni(){
		return this.dni;
	}
	
	public void setIdAct(Integer id_act){
		this.id_act=id_act;
	}
	
	public Integer getIdAct(){
		return this.id_act;
	}
	
	public void setIdSolucion(Integer id_solucion){
		this.id_solucion=id_solucion;
	}
	
	public Integer getIdSolucion(){
		return this.id_solucion;
	}
}
